package com.xia.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xia.pojo.Reader;
import com.xia.service.ReaderService;
import com.xia.utils.AjaxUtils;
import com.xia.utils.Const;
import com.xia.utils.PageBean;
import com.xia.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/reader")
public class ReaderController {

    @Autowired
    public ReaderService readerService;

    @RequestMapping("/toReader")
    public String toReader() {
        return "readerIndex";
    }

    @RequestMapping("/toAddReader")
    public String toAddReader() {
        return "addReader";
    }

    @RequestMapping("/toEdit")
    public String toEdit(Integer readerId, Model model) {
        Reader reader = readerService.readerQueryById(readerId);
        model.addAttribute(Const.READER, reader);
        model.addAttribute("code", 1);
        return "addReader";
    }

    @PostMapping("/updateReader")
    @ResponseBody
    public String editReader(Reader reader) {

        AjaxUtils ajaxUtils = new AjaxUtils();
        try {
            readerService.updateReader(reader);
            ajaxUtils.setSuccess(true);
            ajaxUtils.setCode(0);
            ajaxUtils.setStatus("0");
            ajaxUtils.setMessage("更新成功！");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxUtils.setSuccess(false);
            ajaxUtils.setCode(-1);
            ajaxUtils.setStatus("-1");
            ajaxUtils.setMessage("更新失败！");
        }

        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(ajaxUtils);
        } catch (Exception e) {
            e.printStackTrace();
            return "{\"code\": -1, \"msg\": \"JSON处理错误\"}";
        }
    }

    @RequestMapping("/readerPage")
    @ResponseBody
    public String readerQueryList(@RequestParam(value = "page", defaultValue = "1") Integer pageNo,
                                  @RequestParam(value = "limit", defaultValue = "5") Integer pageSize,
                                  Integer readerId, String name) {

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("pageNo", pageNo);
        paramMap.put("pageSize", pageSize);
        if (!(readerId == null || readerId == 0)) {
            paramMap.put("readerId", readerId);
        }
        if (StringUtil.isNotEmpty(name)) {
            paramMap.put("name", name);
        }
        PageBean<Reader> readerPageBean = readerService.readerQueryList(paramMap);

        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("msg", "");
        resultMap.put("count", readerPageBean.getTotalSize());
        resultMap.put("data", readerPageBean.getData());
        try {
            return mapper.writeValueAsString(resultMap);
        } catch (Exception e) {
            e.printStackTrace();
            return "{\"code\": -1, \"msg\": \"JSON处理错误\"}";
        }
    }

    @PostMapping("/addReader")
    @ResponseBody
    public String realAddReader(Reader reader) {

        AjaxUtils ajaxUtils = new AjaxUtils();
        try {
            reader.setPassword("123456");
            readerService.readerRegister(reader);
            ajaxUtils.setSuccess(true);
            ajaxUtils.setCode(0);
            ajaxUtils.setStatus("0");
            ajaxUtils.setMessage("新增成功！");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxUtils.setSuccess(false);
            ajaxUtils.setCode(-1);
            ajaxUtils.setStatus("-1");
            ajaxUtils.setMessage("新增失败！");
        }

        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(ajaxUtils);
        } catch (Exception e) {
            e.printStackTrace();
            return "{\"code\": -1, \"msg\": \"JSON处理错误\"}";
        }
    }

    @RequestMapping("/readerQueryById")
    public String readerQueryById(Integer readerId, Model model) {

        Reader reader = readerService.readerQueryById(readerId);
        model.addAttribute(Const.READER, reader);
        return "addReader";
    }

    @RequestMapping("/delReader")
    @ResponseBody
    public String delReader(Integer id) {

        AjaxUtils ajaxUtils = new AjaxUtils();
        try {
            readerService.delReader(id);
            ajaxUtils.setSuccess(true);
            ajaxUtils.setMessage("删除成功！");
            ajaxUtils.setCode(0);
            ajaxUtils.setStatus("0");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxUtils.setSuccess(false);
            ajaxUtils.setMessage("删除失败！");
            ajaxUtils.setCode(-1);
            ajaxUtils.setStatus("-1");
        }

        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(ajaxUtils);
        } catch (Exception e) {
            e.printStackTrace();
            return "{\"code\": -1, \"msg\": \"JSON处理错误\"}";
        }
    }
}